﻿using System;
using System.IO;
namespace DAO
{
    public static class ConnectionString
    {
        public static string Text
        {
            get
            {
                return _strconnection;
            }
            set
            {
                _strconnection = value;
            }
        }

        private static string _strconnection = GetConnect();
        public static string Server { get; set; }
        public static string Database { get; set; }
        public static string UserName { get; set; }
        public static string Password { get; set; }


        public static string GetConnect()
        {
            if (UserName == null)
                using (StreamReader reader = new StreamReader("ConnectString.con"))
                {
                    Server = reader.ReadLine().Split(':')[1];
                    Database = reader.ReadLine().Split(':')[1];
                    UserName = reader.ReadLine().Split(':')[1];
                    Password = reader.ReadLine().Split(':')[1];
                }
            if (UserName != "")
                return String.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", Server, Database, UserName, Password);
            else
                return String.Format("Data Source={0};Initial Catalog={1};Integrated Security=True", Server, Database);

        }
        public static void WriteFile(string server, string data, string uid, string pass)
        {
            using (StreamWriter writer = new StreamWriter("ConnectString.con"))
            {
                writer.WriteLine("Server:" + server);
                writer.WriteLine("Database:" + data);
                writer.WriteLine("UserName:" + uid);
                writer.WriteLine("PassWord:" + pass);
                writer.Close();
            }
        }
    }
}
